<template>
  <div :class="{'mobile-detail-wrap':showMobilePage}">
    <div class="sign-detail-container no-margin mobile-radius">
      <template v-if="!permissionDenied">
        <ProgressStep v-if="!showMobilePage" :mode="2" :step-list="stepList" :order-status="stepActiveId" :is-show-correct="false" :is-expedited="invoiceAbandonInfo.isExpedited"/>
        <explanation :explanation="explanationObj" :countdown="true" @queryProcessData="queryProcessData" />
        <div v-loading="infoLoading" class="sign-detail-content small-padding">
          <el-image v-if="isRevoke" :src="revokeImg" class="revoke-img"/>
          <div style="margin-bottom: 32px;">
            <Title title="申请信息" style="margin:0 0 10px 0"/>
            <InvoiveAbandonModule :detail-info="invoiceAbandonInfo" />
          </div>
          <div style="position: relative;">
            <InvoiceAndContract
              ref="InvoiceSelect"
              :related-contract-style="{position: 'absolute',top: '-7px',right: '24px'}"
              :type="2"
              :is-abandon="true"
              :is-edit="false"
              hide-all-operation
              title="需要作废的发票"
              show-empty />
          </div>
        </div>
        <ExamineAndApprove
          ref="examineAndApproveRef"
          :disabled="infoLoading"
          :process-id="processId"
          :main-id="invoiceAbandonId"
          :version="version"
          :get-comments-api="getInvoiceAbandonComments"
          :comment-add-api="invoiceAbandonCommentAdd"
          :process-revocation-api="invoiceAbandonProcessRevocation"
          :process-recall-api="invoiceAbandonProcessRecall"
          :process-audit-api="invoiceAbandonProcessAudit"
          :create-user="createUser"
          :business-type="BUSINESS_TYPE.INVOICEABNDON"
          :process-type="PROCESS_TYPE.ABANDON_INVOICE"
          :audit-schedule-data="auditScheduleData"
          :senders="senders"
          :default-aduitor-list="[{id: invoiceAbandonInfo.proposerId, userName: invoiceAbandonInfo.proposer}]"
          :approval-module-type="APPROVAL_MODULE_TYPE.INVOICE_ABANDON"
          :is-apply="isApply"
          redirect-url="/businessPage/invoiceAbandonDetail"
          detail-page-router-name="invoiceAbandonDetail"
          edit-page-router-name="editInvoiceAbandon"
          @toBack="toBack"
          @getAuditSchedule="getAuditSchedule"
          @queryProcessData="queryProcessData"
          @handlePressSend="handlePressSend"
          @handleCreateNew="handleCreateNew"
          @handleSendMsg="handleSendMsg"
          @handleSender="handleSender"
          @handleModify="handleModify('editInvoiceAbandon',{id: invoiceAbandonId},{remodifyId: processId})"
          @getHistoryDetail="getHistoryDetail"
          @freshDetail="queryInfo"
          @handleCancelPress="handleCancelPress"
        />
      </template>
      <DeniedPlaceholder v-else />
      <ding-ding-dialog
        ref="pressSendDialogRef"
        :btn1="'确认加急'"
        :btn2="'取消'"
        :show-send-checkbox="false"
        :select-with-user-load="false"
        :default-list="pressDefaultList"
        redirect-url="/businessPage/invoiceAbandonDetail"
        summary-title="加急原因"
        title="催办通知"
        @handelOK="toSend" />
    </div>
  </div>
</template>
<script>
// 组件
import ListTab from '@/components/ListTab';
import ProgressStep from '@/components/CreateOrder/orderDetail/step';
import Title from '@/components/Title';
import explanation from '@/components/Common/explanation';
import InvoiceAndContract from '@/components/CreateOrder/InvoiceAndContract';
import InvoiveAbandonModule from './module/invoiveAbandonModule';
import ExamineAndApprove from '@/components/Common/ExamineAndApprove';

// 工具函数
import { mapState } from 'vuex';
import { PROGRESS_STEP } from '@/utils/const/energy';
import { hasRouteAuth } from '@/utils/permission';
import { BUSINESS_TYPE, PROCESS_TYPE } from '@/utils/const/process';
import { APPROVAL_MODULE_TYPE } from '@/utils/const';

// API
import {
  getAbandonDetail, invoiceAbandonCommentAdd, invoiceAbandonProcessRecall,
  getInvoiceAbandonComments, invoiceAbandonProcessRevocation, invoiceAbandonProcessAudit
} from '@/api/invoiceAbandon';
import schedule from '@/mixins/schedule';
import revokeImg from '@/assets/auditProcess/process_status_3.png';

export default {
  components: {
    ListTab, ProgressStep, Title,
    InvoiceAndContract, InvoiveAbandonModule,
    explanation, ExamineAndApprove
  },
  mixins: [schedule],
  data() {
    return {
      APPROVAL_MODULE_TYPE,
      revokeImg,
      BUSINESS_TYPE, PROCESS_TYPE,
      invoiceAbandonId: '', // 签约单id
      processId: '', // 流程id
      invoiceAbandonInfo: { },
      infoLoading: false,
      permissionDenied: false, // 详情页是否可查看
      PROGRESS_STEP,
      // api
      invoiceAbandonProcessRevocation,
      invoiceAbandonCommentAdd,
      getInvoiceAbandonComments,
      invoiceAbandonProcessRecall,
      invoiceAbandonProcessAudit,
      stepActiveId: PROGRESS_STEP.PENDING,
      isRevoke: false
    };
  },
  computed: {
    ...mapState({
      'userInfo': state => state.user.userInfo,
      'showMobilePage': state => state.app.showMobilePage
    }),
    stepList() {
      return [
        { name: '待提交', id: PROGRESS_STEP.PENDING },
        { name: '审批中', id: PROGRESS_STEP.UNDER_REVIEW },
        { name: '已完成', id: PROGRESS_STEP.FINISHED }
      ];
    },
    createUser() {
      let user = { id: this.invoiceAbandonInfo.creatorId, userName: this.invoiceAbandonInfo.creator };
      if (this.userInfo.id === this.invoiceAbandonInfo.creatorId) {
        user = this.userInfo;
      }
      return user;
    },
    isApply() { // 申请人
      const proposerId = this.invoiceAbandonInfo && this.invoiceAbandonInfo.proposerId ? this.invoiceAbandonInfo.proposerId : '';
      return proposerId && this.userInfo.id === proposerId;
    }
  },
  created() {
    this.initData();
  },
  mounted() {
    if (this.$route.query.openComment) {
      this.$refs.examineAndApproveRef.isShowProcess = true;
    }
  },
  methods: {
    hasRouteAuth,
    initData() {
      this.initRouteData();
      this.queryProcessData(); // 流程数据
    },
    initRouteData() {
      this.invoiceAbandonId = this.$route.query.id;
      this.processId = this.$route.query.processId;
      if (!this.invoiceAbandonId || !this.processId) {
        this.$message.warning('无法获取ID');
        const timer = setTimeout(() => {
          this.$router.back();
          clearTimeout(timer);
        }, 1500);
      }
    },
    // 获取基本信息
    queryInfo() {
      this.infoLoading = true;
      getAbandonDetail({ id: this.invoiceAbandonId }).then(res => {
        this.handleProcessCheck(res.data.processId);
        this.handleDetailData(res);
      }).catch(err => {
        if (err.code === 403) {
          this.permissionDenied = true;
        }
        this.infoLoading = false;
      });
    },
    // 处理详情接口返回的数据
    handleDetailData(res) {
      this.infoLoading = false;
      if (res.code === 200) {
        if (res.data.invoiceList) {
          this.$refs.InvoiceSelect && this.$refs.InvoiceSelect.setDefault(res.data.invoiceList);
        }
        this.commentCount = res.data.commentCount;
        if (res.data.processStatus === 3) { // 3为撤销
          this.stepActiveId = PROGRESS_STEP.PENDING;
          this.isRevoke = true;
        } else {
          this.stepActiveId = res.data.processStatus;
          this.isRevoke = false;
        }
        this.invoiceAbandonInfo = res.data;
      }
    },
    queryProcessData() {
      this.queryInfo();
      this.getAuditSchedule();
      this.$nextTick(() => {
        this.$refs.examineAndApproveRef && this.$refs.examineAndApproveRef.getProcessList();
      });
    },
    toBack() {
      if (this.$route.query.fromTodo) {
        this.$router.go(-1);
      } else {
        this.$router.push({ name: 'invoiceAbandon' });
      }
    },
    getAuditSchedule() {
      this.requestAuditSchedule(this.processId);
    },
    // 再次发起（复制）新打开页面
    handleCreateNew() {
      const routeUrl = this.$router.resolve({
        name: 'editInvoiceAbandon',
        params: { id: this.invoiceAbandonId },
        query: { reapplyId: this.processId, createNew: 1 }
      });
      window.open(routeUrl.href, '_blank');
    }
  }
};
</script>
<style lang="scss" scoped>
	.sign-detail-container{
		margin:	16px;
		background: #fff;
		min-height: calc(100vh - 64px);
		.sign-detail-content{
      position: relative;
			padding: 8px 24px 56px;
		}
    &::v-deep {
      .explanation-container{
        .explanation{
          padding-left: 32px!important;
        }
      }
    }
    .revoke-img{
      position: absolute;
      width: 80px;
      height: 80px;
      right: 56px;
      top: 33px;
      z-index: 999;
    }
    .list-tab{
      padding: 0 0 0 10px !important;
      &::v-deep {
        .item-content{
          padding: 16px 0 8px 0 !important;
        }
      }
    }
	}
  .list-tab{
    padding: 0 !important;
  }
	::v-deep .el-drawer {
			min-width: 300px;
		}
		::v-deep .el-form-item__label {
			color: #999;
		}
		::v-deep .el-form-item {
			margin-bottom: 0 !important;
		}
		::v-deep .explanation {
			margin-bottom: 0;
		}
		.sign-detail-container {
			margin: 16px 16px 0 16px;
			background-color: #fff;
			padding-bottom: 55px;
			min-height: calc(100vh - 64px);
		}
</style>
